#region imported Namespaces
using System;
using System.Data;

using DataAccess = PHSRAG.Utility.DataAccess;
using SalaryDistribConstants = PHSRAG.Insight21.Personnel.Constants.SalaryDistributionsConstants;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	#region ExternalSalaryDistribution
	/// <summary>
	/// The ExternalSalaryDistribution class is the facade over 
	/// the details for a single external salary distribution.
	/// </summary>
	public class ExternalSalaryDistribution:SalaryDistribution
	{		
		#region Public Properties
		/// <summary>
		/// Get/Set the external fund source on which the salary distribution is valid
		/// </summary>
		public string ExternalFundSource
		{
			get
			{
				object fundSource = distribRow[SalaryDistribConstants.ExternalFundSourceNameColumnName];
				return (fundSource==DBNull.Value)?string.Empty:(string)fundSource;
			}
			set
			{
				distribRow[SalaryDistribConstants.ExternalFundSourceNameColumnName] = value;
			}
		}
		/// <summary>
		/// Get the fund title
		/// Return an empty string if there is no title for the fund
		/// </summary>
		public override string Title
		{
			get
			{
				object title = distribRow[SalaryDistribConstants.ProjectTitleColumnName];
				return (title==DBNull.Value)?string.Empty:(string)title;
			}
			set
			{
				distribRow[SalaryDistribConstants.ProjectTitleColumnName] = value;
			}
		}
		#endregion

		#region Class methods
		/// <summary>
		/// Get the list of external fund types
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <returns>Datatable with the fund types and the respective IDs</returns>
		public static DataTable GetExternalFundSource(string connectionString)
		{
			return Utility.DataAccess.GetDataTable
				(connectionString,Constants.GetExternalFundSourceSPROCName,null);
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Intialize the base class with the dataRow
		/// </summary>
		/// <param name="dataRow">DataRow with External Salary distribution data</param>
		public ExternalSalaryDistribution(DataRow dataRow): base(dataRow)
		{
		}
		#endregion

		#region Public overridden methods
		/// <summary>
		/// Add a new external salary distribution to the database
		/// Build the sqlParam array with the params specific to external salary distribution
		/// Call the base(SalaryDistribution) AddDistribution method
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		public override void AddDistribution(string connectionString,int domainUserID)
		{
			ValidationsHelper.ValidateEffectiveDate
				(distribRow.Table.DataSet,Convert.ToDateTime(EffectiveDate));

			BuildSQLParamArray();

			base.AddDistribution(connectionString,domainUserID);
		}
		/// <summary>
		/// Edit an existing external salary distribution data in the database
		/// Build the sqlParam array with the params specific to external salary distribution
		/// Call the base(SalaryDistribution) UpdateDistribution method
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		public override void UpdateDistribution(string connectionString,int domainUserID)
		{
			ValidationsHelper.ValidateEffectiveDate
				(distribRow.Table.DataSet,Convert.ToDateTime(EffectiveDate));

			BuildSQLParamArray();

			base.UpdateDistribution(connectionString,domainUserID);
		}
		#endregion

		#region Private method
		/// <summary>
		/// Build the sql param array with the params specific to external salary distribution
		/// </summary>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		private void BuildSQLParamArray()
		{
			sqlParams = Utility.DataAccess.BuildSqlParameterArray
				("@externalFundSourceName",SqlDbType.VarChar,ExternalFundSource
				,"@title",SqlDbType.VarChar,Title);
		}
		#endregion
	}
	#endregion
}